﻿using GameControler.Emgu;
using PlatformCommon.Manager;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace GameControler.Game
{
    public class GoMainView : ViewObject
    {

        public override string FloderName
        {
            get { return "GoMainView"; }
        }

        public GoMainView()
            : base()
        {

        }

        public Point? ClickPoint
        {
            private set;
            get;
        }

        public override bool IsCurrentView(System.Drawing.Bitmap intputbmp)
        {
            var teamtemplate = new List<TemplateView>();
            var rmd = new MatchData();
            foreach (var item in this.Templates)
            {
                var md = JilyEmgu.MatchResult(intputbmp, item, 0.5);
                if (md.IsMatch)
                {
                    if (teamtemplate.Count == 0)
                    {
                        rmd = md;
                        teamtemplate = new List<TemplateView>(this.Templates.Where(i => !string.IsNullOrWhiteSpace(i.Team) && i.Name != item.Name && i.Team == item.Team));

                        if (teamtemplate.Count == 0)
                        {
                            ClickPoint = rmd.Point;

                            Log4Wrap.Logger.Info("GoMain界面匹配" + "___" + rmd.TemplateView.Name + "___" + ClickPoint.Value);

                            return true;
                        }
                    }
                    // mds.Add(md);
                }
            }

            if (teamtemplate.Count != 0)
            {
                foreach (var item in teamtemplate)
                {
                    var x = rmd.Point.X;
                    var y = rmd.Point.Y;
                    try
                    {
                        using (var clipbitmap = intputbmp.Clone(new Rectangle(x, y, item.Image.Width, item.Image.Height), intputbmp.PixelFormat))
                        {
                            var md = JilyEmgu.MatchAllResultRGB(clipbitmap, item);
                            if (!md.IsMatch)
                            {
                                return false;
                            }
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }

                ClickPoint = rmd.Point;
                Log4Wrap.Logger.Info("GoMain界面匹配" + "___" + rmd.TemplateView.Name + "___" + ClickPoint.Value);
                return true;
            }
            //foreach (var item in this.Templates)
            //{
            //    var md = JilyEmgu.MatchResult(intputbmp, item);
            //    if (md.IsMatch)
            //    {
            //        ClickPoint = md.Point;
            //        return true;
            //    }
            //}
            return false;
        }

    }

}
